function [tSer,tsfilename] = parseTextFile(filename)
% Find this file
filename
tSer = {};
file = dir(filename);
fid         = fopen(['../LoudnessSub/' file(1).name]);
rowdata     = textscan(fid,'%s','Delimiter','\n','BufSize',1000000);
namedata    = textscan(rowdata{1}{1},'%s','Delimiter','\t','BufSize',65536);
infodata    = textscan(rowdata{1}{2},'%s','Delimiter','\t','BufSize',65536);

fid         = fopen(['../LoudnessSub/' file(1).name]);
strdata     = textscan(fid,'%s','Delimiter','\t','BufSize',65536);
strdata     = strdata{1};
col =1;
  

Questions = {
'Q9.AF0_Question Name'
'Q11.AF0_Question Name'
'Q13.AF0_Question Name'
'Q15.AF0_Question Name'
'Q17.AF0_Question Name'
'Q19.AF0_Question Name'
'Q21.AF0_Question Name'
'Q23.AF0_Question Name'
'Q27.AF0_Question Name'
'Q28.AF0_Question Name'
'Q29.AF0_Question Name'
'Q30.AF0_Question Name'
'Q31.AF0_Question Name'
'Q32.AF0_Question Name'
'Q33.AF0_Question Name'
'Q34.AF0_Question Name'};

FileQs = {
'Q9.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q11.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q13.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q15.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q17.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q19.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q21.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q23.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q27.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q28.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q29.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q30.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q31.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q32.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q33.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
'Q34.AB3_PlayedFile[If a multimedia file was played it''s path is indicated here]'
};    


ContDataQs = {
'Q9.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q11.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q13.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q15.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q17.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q19.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q21.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q23.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q27.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q28.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q29.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q30.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q31.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q32.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q33.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
'Q34.AB3_ContinuousData[Three rows of data indicating time x and y positions as multimedia file unfolds] '
};



age = getString(namedata{1},infodata{1},'Q4.AB3_Age');
mt = getString(namedata{1},infodata{1},'Q4.AB4_Years of Instrument Lessons');
gender = getString(namedata{1},infodata{1},'Q4.AB6_Gender');
id = getString(namedata{1},infodata{1},'Q2.AF2_AnswerBox');
col = 1;
for i = 1:length(Questions)
    Answers(i) = {getString(namedata{1},infodata{1},Questions{i})};
    ContData(i) = {getString(namedata{1},infodata{1},ContDataQs{i})};
    FileData(i) = {getString(namedata{1},infodata{1},FileQs{i})};
    commas = strfind(Answers{i}, ',');
    commas2 = strfind(FileData{i}, ',');
    tsp = Answers{i}(1:commas(1)-1);
    trackingsp{i} = strrep(tsp, 'LoudnessTracking','');
    
    fn(i) =  {Answers{i}(commas(1)+1:commas(2)-1)};
    fn2(i) =  {FileData{i}(1:commas2(1)-1)};
    fprintf('Parsed %s\t%s\t %d\n ', fn{i},fn2{i},strcmp(fn{i},fn2{i}));
    if ~isempty(ContData{i}) && strcmp(fn{i},fn2{i})
        tSer(col) = {parseColumn(ContData{i},fn2{i},file(1).name,trackingsp{i},age,mt)};  
        fprintf('Parsed %s\t%s\t %d\n ', fn{i},fn2{i},strcmp(fn{i},fn2{i}));
        heard = fn2;
        tSer{col}.DataInfo.UserData.age = age;        
        tSer{col}.DataInfo.UserData.mt = mt;
        tSer{col}.DataInfo.UserData.heard = heard;
        tSer{col}.DataInfo.UserData.gender = gender;
        tSer{col}.DataInfo.UserData.id = id;
        
        col = col + 1;
    else
        disp('Problem');
    end
end




function ts = parseColumn(strdata,audiofilename,subjfilename,trackingsp,age,mt)

cols = textscan(strdata,'%s%n%n','Delimiter','');
x = cols{2}; 
y = cols{3};
timeCol = cols{1};


if strcmp(audiofilename,'OlsenRamps.wav')
  disp('Saving...');
  fid = fopen([subjfilename '-' trackingsp '-' age '-' mt '.txt'],'w'); 
  for i = 1:length(timeCol)
  fprintf(fid,'%s\t%s\n', timeCol{i},  num2str(x(i)));
  end
  fclose(fid);
end

for i = 1:length(timeCol)
  lastbit = floor(str2num(timeCol{i}(end-1:end))/30*60);
  times(i) = {[timeCol{i}(2:end-3) '.' sprintf('%02i',lastbit)]};
end

% Time
times = datevec(times);
times = times(:,6) + 60*times(:,5);

% Create timeseries
ts = timeseries(x,times);
filename = strrep(subjfilename, '../LoudnessSub/LoudnessTrackingMar09','');
ts.Name = ['Response-' trackingsp '-' filename];
ts.DataInfo.UserData.audiofn = audiofilename;
ts.DataInfo.UserData.fn = filename;



function val = getString(namedata,infodata,nameStr)

for i = 1:length(namedata)
    if strcmp(deblank(namedata{i}),deblank(nameStr))
        val = infodata{i};
    end
end
